//
//  EncyViewController.m
//  安好
//
//  Created by lianchuang on 14-11-10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "EncyViewController.h"
#import "EncyCell.h"
#import "EncyModal.h"
#import "IllnessViewController.h"
#import "HospitalViewController.h"
#import "MedicalOfficeViewController.h"
#import "DoctorViewController.h"
#import "DrugViewController.h"

#define CellLineColor [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface EncyViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIImageView *_headerLineView;
}

@end

@implementation EncyViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
    
}
#pragma mark - 导航条的布局
-(void)makeNavBar
{
    self.navigationItem.title = @"百科";
    //贴 返回 按钮的图片
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 加载数据
-(void)loadData
{
    [self showActionView];
    self.encyArray = [NSMutableArray arrayWithCapacity:0];
    self.totalItemArray = [NSMutableArray arrayWithCapacity:0];
    NSArray * introductionArray = @[@"按疾病查找相关信息",@"按医院查找相关信息",@"按科室查找相关信息",@"按医生查找相关信息",@"按药品查找相关信息"];
    NSArray * categoryArray = @[@"疾病",@"医院",@"科室",@"医生",@"药品"];
    
    for (NSInteger i = 0; i < categoryArray.count; i++) {
        NSString *count = [NSString stringWithFormat:@"共有1972 %@",categoryArray[i]];
        NSString *picUrl = [NSString stringWithFormat:@"百科_%@.png",categoryArray[i]];
        NSDictionary *dic = @{@"category":categoryArray[i],@"introduction":introductionArray[i],@"picUrl":picUrl};
        [self.encyArray addObject:dic];
        [self.totalItemArray addObject:count];
    }
    //
    //postUrl
    NSString *postURL = [NSString stringWithFormat:@"%@wiki/",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);//失败
        }else
        {
            NSDictionary *encyDic = [responseObject objectForKey:@"data"];
            
            NSArray *allkey = [encyDic allKeys];
            //对keys排序
            NSArray *arrNum = [allkey sortedArrayUsingSelector:@selector(compare:)];
            [self.totalItemArray removeAllObjects];
            for (NSInteger i = 0; i < arrNum.count; i++) {
                NSDictionary * dic = [encyDic objectForKey:arrNum[i]];
                NSString *count  = dic[@"count"];
                [self.totalItemArray addObject:count];
            }
            //刷新table
            [self.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];

    }];
}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];


}
#pragma mark - tableView的代理方法
/**
 *  设置 各行cell高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth == 414){
        return 85;
    }else{
        return 64;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.encyArray.count;
}
#pragma mark --cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EncyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (kScreenWidth == 414){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EncyCell" owner:self options:nil] lastObject];
  
        }else{
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EncyCell" owner:self options:nil] firstObject];

        }
    }
    /**
     * cell 上加载数据
     */
    cell.totalLable.text = self.totalItemArray[indexPath.row];
    [cell.itemImgView setImage:[UIImage imageNamed:[self.encyArray[indexPath.row] objectForKey:@"picUrl"]]];
    cell.itemImgView.clipsToBounds = YES;
    cell.itemImgView.layer.cornerRadius = 3;
    cell.titleLable.text = [self.encyArray[indexPath.row] objectForKey:@"category"];
    cell.infoLable.text = [self.encyArray[indexPath.row] objectForKey:@"introduction"];
    if (IPhone6Plus) {
        cell.totalLable.width = 130;
        cell.totalLable.right = cell.contentView.right-15;;
        
    }
    
    
    //底线
    cell.bottomLineView.backgroundColor = CellLineColor
    if (indexPath.row != self.encyArray.count - 1)
    {
        if (kScreenWidth == 414){
            cell.bottomLineView.frame = CGRectMake(90, 84.5,kScreenWidth-90 - 5, 0.5);
 
        }else{
        cell.bottomLineView.frame = CGRectMake(70, 63.5,kScreenWidth-70 - 5, 0.5);

        }
    }
    else
    {
        if (kScreenWidth == 414){
            cell.bottomLineView.frame = CGRectMake(0, 84.5, kScreenWidth, 0.5);

        }else{
        cell.bottomLineView.frame = CGRectMake(0, 63.5, kScreenWidth, 0.5);

        }
    }
    if (indexPath.row == 0) {
        cell.topLineView.backgroundColor =CellLineColor
        cell.topLineView.frame = CGRectMake(0, 0, kScreenWidth, 0.3);
    }
    
    return cell;
}
#pragma mark --选中跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            IllnessViewController *illness = [[IllnessViewController alloc] init];
            [self.navigationController pushViewController:illness animated:YES];
        
        
        }
            break;
        case 1:{
        HospitalViewController *hospital = [[HospitalViewController alloc] init];
            [self.navigationController pushViewController:hospital animated:YES];
        }
            break;
        case 2:{
        MedicalOfficeViewController *office = [[MedicalOfficeViewController alloc] init];
            [self.navigationController pushViewController:office animated:YES];
        }
            break;
        case 3:{
        DoctorViewController *doctor = [[DoctorViewController alloc] init];
            [self.navigationController pushViewController:doctor animated:YES];
        }
            break;
        case 4:{
        DrugViewController *drug = [[DrugViewController alloc] init];
            [self.navigationController pushViewController:drug animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}


@end
